/**
 * 新建签单
 * Created by douxc on 2015/4/22.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('VerifyAddCtrl', ['$scope', 'MHService', '$http', 'Upload', function ($scope, MHService, $http, Upload) {
      //获取所有的单据类型
      MHService.getReq({url: MHService.URL.invoice_type}, function (data) {
        $scope.verifyType = data['datas'];
        $scope.selected = $scope.verifyType[0];
      });

      //上传图片
      $scope.onFileSelect = function($files) {
        if($files.length==0) {
          return;
        }
        var file = $files[0];
        if(file.type=='image/gif'|| file.type=='image/jpg'|| file.type=='image/jpeg') {
          Upload.upload({url: MHService.URL.img_upload, method: 'POST', file: file}).success(function(res) {
            console.log(res.data);
            $scope.img = res.data;
          });
        } else {
          alert('只支持gif/jpg/jpeg格式的图片，请重新选择。');
          return;
        }
      }

      //提交签单
      $scope.saveVerify = function () {
        var req = {
          invoicesid: $scope.selected.id,
          cost: $scope.feeType,
          amount: $scope.fee,
          description: $scope.instruction,
          image: $scope.img
        };
        MHService.postReq({url: MHService.URL.save_verify, _req: req}, function (data) {
          if (data['success']) {
            //成功
            MHService.Toast('提交成功');
            history.go(-1);
          }
        });
      };
    }]);
  };
});
